package com.example.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.admin.common.result.R;
import com.example.admin.entity.SysUser;
import com.example.admin.model.dto.PasswordDTO;
import com.example.admin.model.dto.SysUserRequest;
import com.example.admin.model.dto.UserDTO;
import com.example.admin.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取用户列表")
    @PostMapping("/list")
    public R<IPage<SysUser>> getUserList(@RequestBody @ApiParam("分页参数") SysUserRequest request) {
        return R.ok(userService.getUserPage(request));
    }

    @ApiOperation("新增用户")
    @PostMapping("/add")
    public R<Void> addUser(@RequestBody @ApiParam("用户信息") UserDTO userDTO) {
        userService.addUser(userDTO);
        return R.ok();
    }

    @ApiOperation("更新用户")
    @PostMapping("/update")
    public R<Void> updateUser(@RequestBody @ApiParam("用户信息") UserDTO userDTO) {
        userService.updateUser(userDTO);
        return R.ok();
    }

    @ApiOperation("删除用户")
    @GetMapping("/delete")
    public R<Void> deleteUser(@ApiParam("用户ID") @RequestParam Long id) {
        userService.deleteUser(id);
        return R.ok();
    }

    /**
     * 根据用户id获取用户
     * @param id
     * @return
     */
    @ApiOperation("根据用户id获取用户")
    @GetMapping("/get")
    public R<SysUser> get(@RequestParam Long id) {
        return R.ok(userService.getUserById(id));
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @ApiOperation("重置密码")
    @GetMapping("/reset-password")
    public R<Void> resetPassword(@RequestParam Long id) {
        userService.resetPassword(id);
        return R.ok();
    }

    /**
     * 获取当前登录用户
     * @return
     */
    @ApiOperation("获取当前登录用户")
    @GetMapping("/profile")
    public R<SysUser> getProfile() {
        return R.ok(userService.getCurrentUser());
    }

    @ApiOperation("更新个人信息")
    @PostMapping("/profile")
    public R<Void> updateProfile(@RequestBody @ApiParam("个人信息") UserDTO userDTO) {
        userService.updateProfile(userDTO);
        return R.ok();
    }

    @ApiOperation("修改密码")
    @PostMapping("/password")
    public R<Void> updatePassword(@RequestBody @ApiParam("密码信息") PasswordDTO passwordDTO) {
        userService.updatePassword(passwordDTO);
        return R.ok();
    }
} 